<?php


namespace app\api\model;

use think\Model;
use think\Db;


class Device extends Model{


    //返回原有数据  不自动进行时间转换
    public function getCreateTimeAttr($time)
    {
        return $time;
    }

    protected function getUtimeAttr($vlue){
        return date("Y-m-d H:i:s",$vlue);
    }

    protected function getLastUseTimeAttr($vlue)
    {
        return date("Y-m-d H:i:s",$vlue);
    }

    /*
    * Notes: 更新设备管理数据
    * Create Date: 2018/10/29
    * Create Time: 16:53
    * Create by :lijie
    * $con : 条件
    * $save : 更新数据
    */
    public function updateDevice($save,$con){
        $result = $this
                ->where($con)
                ->update($save);
        return $result;
    }

    /**
     * Notes:  设备详情
     * Create Date: 2018/10/3
     * Create by :topqiang
     */
    public function deviceInfo($param)
    {
        return $this->where($param)->find();
    }

    /**
     * [delCoupon description]
     * @author topqiang
     * @DateTime 2018-09-02T11:55:46+0800
     * @param    [type]                   $id [description]
     * @return   [type]                       [description]
     */
    public function delDevice($id)
    {
        return $this->isUpdate(true)->save(['status'=>9],['id'=>$id]);
    }
    
    /**
     * 
     * @author topqiang
     * @DateTime 2018-09-02T11:55:35+0800
     * @param    [type]                   $pageNum [description]
     * @param    [type]                   $listNum [description]
     * @param    [type]                   $w       [description]
     * @param    [type]                   $lat     [description]
     * @param    [type]                   $lnt     [description]
     * @return   [type]                            [description]
     */
    public function deviceListByLatLon($pageNum,$listNum,$w,$lat,$lnt){
        $reslt["list"] = $this
            ->field("*,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($lat*PI()/180-lat*PI()/180)/2),2)+COS($lat*PI()/180)*COS(lat*PI()/180)*POW(SIN(($lnt*PI()/180-lon*PI()/180)/2),2)))*1000) AS distance")
            ->where($w)
            ->page($pageNum,$listNum)
            ->order("distance asc")
            ->select();
        foreach ($reslt["list"] as $key => $value) {
            $reslt["list"][$key]['qrcodeurl'] = "//mini.kuchengxiche.cn/".$value['qrcodeurl'];
        }
        $reslt["total"] = $this
            ->where($w)
            ->count("id");
        return $reslt;
    }
    public function deviceList($pageNum,$listNum,$w){
        $reslt["list"] = $this
            ->where($w)
            ->page($pageNum,$listNum)
            ->select();
        foreach ($reslt["list"] as $key => $value) {
            $reslt["list"][$key]['qrcodeurl'] = "//mini.kuchengxiche.cn/".$value['qrcodeurl'];
        }
        $reslt["total"] = $this
            ->where($w)
            ->count("id");
        return $reslt;
    }

    /*
    * Notes: 判断牌照号是否已经存在
    * Create Date: 2018/10/12
    * Create Time: 下午2:21
    * Create by :张福来
    * param :
    */
    public function carjudge($choseCarLi){
        $result = Db::name("carinfor")
                ->field("licenseplate")
                ->where($choseCarLi)
                ->select();
        return $result;
    }
}